package com.matt.service.impl;

import com.google.common.collect.Lists;
import com.matt.domain.*;

import com.matt.foundation.query.OperationLogQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.VaccineNotificationsRepository;
import com.matt.service.VaccineNotificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("VaccineNotificationsService")
public class VaccineNotificationsServiceImpl implements VaccineNotificationsService {

    @Autowired
    private VaccineNotificationsRepository vaccineNotificationsRepository;

    @Override
    public void addVaccineNotifications(VaccineNotifications vaccineNotifications) {
        vaccineNotificationsRepository.save(vaccineNotifications);
    }

    @Override
    public Page<VaccineNotifications> selVaccineNotifications(String name, Integer pageNum, Integer pageSize) {

        // 使用 Specification 构建动态查询条件
        Specification<VaccineNotifications> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();


            // 判断 name 是否为空，不为空则添加到查询条件中
            if (name != null && !name.isEmpty()) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + name + "%"));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };

        Sort sort = Sort.by(Sort.Order.asc("age"));


        Page<VaccineNotifications> all = vaccineNotificationsRepository.findAll(specification, PageRequest.of(pageNum, pageSize,sort));

        return all;
    }

    @Override
    public void delVaccineNotifications(String id) {
        vaccineNotificationsRepository.deleteById(id);
    }

}
